<template>
  <div class="login-wrap">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm"  class="demo-ruleForm">
        <h1 class="title">用户登录</h1>
      <el-form-item prop="username">
        <el-input  type="username" v-model="ruleForm.username" autocomplete="off" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input  type="password" v-model="ruleForm.checkPass" autocomplete="off" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button style="width: 100%;" type="primary" round @click="submitForm('ruleForm')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'))
      } else {
        setTimeout(() => {
          if (this.ruleForm.username !== '') {
            this.$refs.ruleForm.validateField('username')
          }
        })
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        setTimeout(() => {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass')
          }
        })
        callback()
      }
    }
    return {
      ruleForm: {
        username: '',
        checkPass: ''
      },
      rules: {
        username: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style>
.login-wrap{
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;

}
.demo-ruleForm{
  border-radius: 10%;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0,0.1);
}
.title{
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

</style>
